package com.yisu.auction.manager.core.service.impl;

import com.yisu.auction.ewf.common.web.EasyUIGrid;
import com.yisu.auction.ewf.entity.AppSignGoods;
import com.yisu.auction.ewf.entity.AppSignManager;
import com.yisu.auction.ewf.entity.GoodsInfo;
import com.yisu.auction.manager.core.mapper.AppSignGoodsMapper;
import com.yisu.auction.manager.core.mapper.AppSignManagerMapper;
import com.yisu.auction.manager.core.mapper.GoodsInfoMapper;
import com.yisu.auction.manager.core.service.IAppSignService;
import com.yisu.auction.manager.domain.request.systemConfigPackage.AppSignRequest;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * author: 黄萧林
 * Business Description：
 * createTime：2017/9/4
 */
@Service("IAppSignService")
public class AppSignServiceImpl implements IAppSignService {
    @Resource
    private AppSignManagerMapper appSignManagerMapper;
    @Resource
    private GoodsInfoMapper goodsInfoMapper;
    @Resource
    private AppSignGoodsMapper appSignGoodsMapper;

    /**
     * author:黄萧林
     * Business Description:分页获取appSign数据
     *
     * @param appSignRequest
     * @return
     */
    @Override
    public EasyUIGrid<AppSignManager> getAppSignInfoByPage(AppSignRequest appSignRequest) {
        List<AppSignManager> appSignManagers = appSignManagerMapper.getAppSignInfoByPage(appSignRequest);
        EasyUIGrid<AppSignManager> gridData = new EasyUIGrid<>();
        gridData.setTotal(appSignRequest.getTotalRecords());
        gridData.setRows(appSignManagers);
        return gridData;
    }

    /**
     * author:黄萧林
     * Business Description:修改appSign数据
     *
     * @param appSignManager
     * @return
     */
    @Override
    public boolean modifyAppSignInfo(AppSignManager appSignManager) {
        AppSignManager appSignManager1 = appSignManagerMapper.selectByPrimaryKey(appSignManager.getId());
        if (appSignManager1 == null) {
            AppSignManager appSignManager2 = new AppSignManager();
            appSignManager2.setCreateDate(new Date());
            appSignManager2.setAppPackageSign(appSignManager.getAppPackageSign());
            appSignManager2.setDescription(appSignManager.getDescription());
            return appSignManagerMapper.insertSelective(appSignManager2) > 0;
        } else {
            appSignManager1.setId(appSignManager.getId());
            appSignManager1.setUpdateDate(new Date());
            appSignManager1.setAppPackageSign(appSignManager.getAppPackageSign());
            appSignManager1.setDescription(appSignManager.getDescription());
            return appSignManagerMapper.updateByPrimaryKeySelective(appSignManager1) > 0;
        }
    }

    /**
     * author:黄萧林
     * Business Description:删除appSign数据
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteAppSignInfo(Long id) {
        return appSignManagerMapper.deleteByPrimaryKey(id) > 0;
    }

    /**
     * author:黄萧林
     * Business Description:获取一条要修改的数据
     *
     * @param id
     * @return
     */
    @Override
    public AppSignManager getAppSign(Long id) {
        return appSignManagerMapper.selectByPrimaryKey(id);
    }

    /**
     * author:赵杰
     * Business Description:获取所有签名列表
     *
     * @return
     */
    @Override
    public List<AppSignManager> getAppSignManagerList() {
        return appSignManagerMapper.getAppSignManagerList();
    }

    /**
     * author:赵杰
     * Business Description:通过signId初始化包商品
     *
     * @param signId
     * @return
     */
    @Override
    public boolean initSignGoodsBySignId(long signId) {
        AppSignManager appSignManager = appSignManagerMapper.selectByPrimaryKey(signId);
        if (appSignManager == null)
            return false;
        List<GoodsInfo> goodsInfos = goodsInfoMapper.getAllGoodsInfo();
        if (!CollectionUtils.isEmpty(goodsInfos)) {
            for (GoodsInfo item : goodsInfos) {
                AppSignGoods appSignGoods = new AppSignGoods();
                appSignGoods.setGoodsId(item.getId());
                appSignGoods.setCreateDate(new Date());
                appSignGoods.setAppPackageSign(appSignManager.getAppPackageSign());
                appSignGoodsMapper.insertSelective(appSignGoods);
            }
            return true;
        }
        return false;
    }
}
